home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gdevsun.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  21KB  |  691 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevsun.c */
  20. /* SunView driver for Ghostscript */
  21. #include "gx.h"            /* for gx_bitmap; includes std.h */
  22.  
  23. #include <suntool/sunview.h>
  24. #include <suntool/canvas.h>
  25. #include <sunwindow/cms_mono.h>
  26. #include <stdio.h>
  27.  
  28. #include "gsmatrix.h"            /* needed for gxdevice.h */
  29. #include "gxdevice.h"
  30. #include "malloc_.h"
  31.  
  32. #ifndef DEFAULT_DPI
  33. #  define DEFAULT_DPI 75        /* Sun standard monitor */
  34. #endif
  35.  
  36. #ifdef A4
  37. #  define PAPER_X 8.27            /* A4 paper */
  38. #  define PAPER_Y 11.69
  39. #endif
  40.  
  41. #ifndef PAPER_X
  42. #  define PAPER_X 8.5            /* US letter paper */
  43. #  define PAPER_Y 11
  44. #endif
  45. /* Procedures */
  46. dev_proc_open_device(sun_open);
  47. dev_proc_sync_output(sun_sync);
  48. dev_proc_close_device(sun_close);
  49. dev_proc_map_rgb_color(sun_map_rgb_color);
  50. dev_proc_map_color_rgb(sun_map_color_rgb);
  51. dev_proc_fill_rectangle(sun_fill_rectangle);
  52. dev_proc_copy_mono(sun_copy_mono);
  53. dev_proc_copy_color(sun_copy_color);
  54. dev_proc_draw_line(sun_draw_line);
  55.  
  56. /* The device descriptor */
  57. private gx_device_procs sun_procs = {
  58.     sun_open,
  59.     gx_default_get_initial_matrix,
  60.     sun_sync,
  61.     gx_default_output_page,
  62.     sun_close,
  63.     sun_map_rgb_color,
  64.     sun_map_color_rgb,
  65.     sun_fill_rectangle,
  66.     gx_default_tile_rectangle,
  67.     sun_copy_mono,
  68.     sun_copy_color,
  69.     sun_draw_line,
  70.     gx_default_get_bits,
  71.     gx_default_get_props,
  72.     gx_default_put_props
  73. };
  74.  
  75. #define CMSNAME    "GHOSTVIEW"        /* SunView colormap name */
  76.  
  77. /* Define the SunView device */
  78. typedef struct gx_device_sun {
  79.     gx_device_common;
  80.     Frame frame;
  81.     Canvas canvas;
  82.     Pixwin *pw;
  83.     struct mpr_data mpr;
  84.     Pixrect    pr;
  85.     int truecolor;            /* use truecolor mapping */
  86.     int freecols;            /* unallocated colors */
  87.     byte *red, *green, *blue;    /* colormap */
  88.     char cmsname[sizeof(CMSNAME)+9];/* color map name */
  89. #if !arch_is_big_endian            /* need to swap bits & bytes */
  90. #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
  91.     byte swap_buf[BUF_WIDTH_BYTES];
  92. #endif
  93. } gx_device_sun;
  94.  
  95. #if !arch_is_big_endian
  96. /* Define a table for reversing bit order. */
  97. static byte reverse_bits[256] = {
  98.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  99.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  100.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  101.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  102.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  103.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  104.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  105.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  106.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  107.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  108.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  109.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  110.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  111.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  112.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  113.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  114. };
  115. #endif
  116.  
  117. /* The instance is public. */
  118. gx_device_sun gs_sunview_device = {
  119.     sizeof(gx_device_sun),
  120.     &sun_procs,
  121.     "sunview",
  122.      (int)(PAPER_X*DEFAULT_DPI), (int)(PAPER_Y*DEFAULT_DPI),    /* x and y extent */
  123.      DEFAULT_DPI, DEFAULT_DPI,    /* x and y density */
  124.     no_margins,
  125.     dci_color(0,0,0),    /* fill in later from display depth */
  126.      0,            /* connection not initialized */
  127. };
  128.  
  129. /* Macro for casting gx_device argument */
  130. #define xdev ((gx_device_sun *)dev)
  131.  
  132. /*
  133.  * The macros below define the colormap configuration used on 8-bit
  134.  * pseudo-color displays.
  135.  */
  136. /*
  137.  * The following macros define the number of bits used to represent rgb colors.
  138.  * The total must not exceed the display depth.
  139.  * Note that the RGB dimensions could have an uneven number of bits assigned
  140.  * to them, but that will cause dithering to not work very well, since
  141.  * gs assumes the dither ramp is the same for all 3 color dimensions.
  142.  *
  143.  * Setting RED_BITS to n will pre-allocate a color-cube of 2^(3n) entries.
  144.  * The remaining entries are allocated on demand for colors requested by
  145.  * sun_map_rgb_color(), until the color map is full. At that point gs will
  146.  * fall back onto dithering using the pre-allocated colors.
  147.  * As a special case, if RED_BITS = GREEN_BITS = BLUE_BITS = 0, only
  148.  * black and white are pre-allocated.
  149.  */
  150. #define RED_BITS    2        /* everything depends on this one */
  151. #define GREEN_BITS    RED_BITS
  152. #define BLUE_BITS    RED_BITS
  153. #define DEPTH        8        /* don't change this */
  154. #define RGB_BITS    (RED_BITS + GREEN_BITS + BLUE_BITS)
  155. /*
  156.  * Smallest # bits per dimension
  157.  */
  158. #define MAX_BITS    RED_BITS
  159. #if (GREEN_BITS > MAX_BITS)
  160. #undef MAX_BITS
  161. #define MAX_BITS    GREEN_BITS
  162. #endif
  163. #if (BLUE_BITS > MAX_BITS)
  164. #undef MAX_BITS
  165. #define MAX_BITS    BLUE_BITS
  166. #endif
  167. /*
  168.  * masks to pull out rgb components
  169.  */
  170. #define BLUE_MASK    ((1 << BLUE_BITS) - 1)
  171. #define GREEN_MASK    ((1 << (BLUE_BITS + GREEN_BITS)) - 1 - BLUE_MASK)
  172. #define RED_MASK    ((1 << (BLUE_BITS + GREEN_BITS + RED_BITS)) - 1 \
  173.              - BLUE_MASK - GREEN_MASK)
  174. /*
  175.  * number of colors on rgb dimensions
  176.  */
  177. #define RED_COLS    (1 << RED_BITS)
  178. #define GREEN_COLS    (1 << GREEN_BITS)
  179. #define BLUE_COLS    (1 << BLUE_BITS)
  180. #define RGB_COLS    (RED_COLS * GREEN_COLS * BLUE_COLS)
  181. #define MAX_COLS    (1 << MAX_BITS)
  182. /*
  183.  * maximum number of colors in map
  184.  */
  185. #define ALL_COLS    (1 << DEPTH)    /* 256 */
  186. #define CMS_SIZE    ALL_COLS    /* cut down to 64 or 128 for
  187.                        more cooperative behaviour */
  188.  
  189. #if (RGB_COLS > CMS_SIZE)        /* one is reserved for the scrollbar */
  190. CMS_SIZE_too_small_for_color_cube
  191. #endif
  192. #if (RGB_BITS < 0) || (RGB_BITS > DEPTH) 
  193. Display_does_not_support_this_many_colors
  194. #endif
  195.  
  196. /*
  197.  * The macros below define the color mapping used on 24-bit true-color
  198.  * displays.
  199.  * FAKE_TRUE_COLOR is used for debugging only.  It simulates a true-color
  200.  * type mapping on an 8-bit pseudo-color display.
  201. #define FAKE_TRUE_COLOR
  202.  */
  203. #ifdef FAKE_TRUE_COLOR
  204. # define TRUE_RED_BITS    3        /* everything depends on this one */
  205. # define TRUE_GREEN_BITS 2
  206. # define TRUE_BLUE_BITS    (DEPTH - TRUE_RED_BITS - TRUE_GREEN_BITS)
  207. #else
  208. # define TRUE_RED_BITS    8        /* everything depends on this one */
  209. # define TRUE_GREEN_BITS TRUE_RED_BITS
  210. # define TRUE_BLUE_BITS    TRUE_RED_BITS
  211. #endif ./* FAKE_TRUE_COLOR */
  212. #define TRUE_DEPTH    (TRUE_RED_BITS + TRUE_GREEN_BITS + TRUE_BLUE_BITS)
  213. /*
  214.  * Masks to pull out rgb components.  Note that the bit order is BGR from
  215.  * high to low order bits.
  216.  */
  217. #define TRUE_RED_MASK    ((1 << TRUE_RED_BITS) - 1)
  218. #define TRUE_GREEN_MASK    ((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS)) - 1 \
  219.              - TRUE_RED_MASK)
  220. #define TRUE_BLUE_MASK    ((1 << (TRUE_RED_BITS + TRUE_GREEN_BITS \
  221.                     + TRUE_BLUE_BITS)) - 1 \
  222.              - TRUE_GREEN_MASK - TRUE_RED_MASK)
  223. /*
  224.  * number of colors on rgb dimensions
  225.  */
  226. #define TRUE_RED_COLS    (1 << TRUE_RED_BITS)
  227. #define TRUE_GREEN_COLS    (1 << TRUE_GREEN_BITS)
  228. #define TRUE_BLUE_COLS    (1 << TRUE_BLUE_BITS)
  229.  
  230. /* Initialize the device. */
  231. private Notify_value destroy_func();
  232. int
  233. sun_open(register gx_device *dev)
  234. {
  235. #ifdef gs_DEBUG
  236. if ( gs_debug['X'] )
  237.     { extern int _Xdebug;
  238.       _Xdebug = 1;
  239.     }
  240. #endif
  241.     if (xdev->frame == (Frame)0)
  242.         xdev->frame =
  243.         window_create(NULL, FRAME, FRAME_LABEL, "ghostscript",
  244.             WIN_WIDTH, min(xdev->width + 24, 900),
  245.             WIN_HEIGHT, min(xdev->height + 36, 900),
  246.             WIN_Y, 0,
  247.             WIN_X, 200,
  248.             0);
  249.     if (xdev->frame == (Frame)0)
  250.         return -1;
  251.     xdev->canvas = window_create(xdev->frame, CANVAS,
  252.             CANVAS_AUTO_EXPAND,        FALSE,
  253.             CANVAS_AUTO_SHRINK,        FALSE,
  254.             CANVAS_WIDTH,            xdev->width,
  255.             CANVAS_HEIGHT,            xdev->height,
  256. #ifndef PRE_IBIS    /* try to use 24-bit visual if OS supports it */
  257.             CANVAS_COLOR24,            TRUE,
  258. #endif
  259.             CANVAS_RETAINED,        FALSE,
  260.         0);
  261.     xdev->pw = canvas_pixwin(xdev->canvas);
  262.  
  263.     switch (xdev->pw->pw_pixrect->pr_depth) {
  264.          static gx_device_color_info mono_ci =
  265.         dci_black_and_white;
  266.          /*
  267.           * If the pre-allocated color cube leaves room for spare entries,
  268.           * tell gs we can render colors exactly.  Otherwise admit our
  269.           * limitations.
  270.           */
  271.          static gx_device_color_info color_ci =
  272. #if (RGB_COLS < CMS_SIZE)
  273.         dci_color(DEPTH, 31, MAX_COLS);
  274. #else
  275.         dci_color(DEPTH, MAX_COLS - 1, MAX_COLS);
  276. #endif
  277.          static gx_device_color_info truecolor_ci =
  278.         dci_color(TRUE_DEPTH,31,4);
  279.     case 1:
  280.          /* mono display */
  281.          xdev->color_info = mono_ci;
  282.          break;
  283. #ifndef FAKE_TRUE_COLOR
  284.     case DEPTH:
  285.          /* pseudo-color display */
  286.          xdev->color_info = color_ci;
  287.          xdev->truecolor = 0;
  288.          break;
  289. #endif /* FAKE_TRUE_COLOR */
  290.     case TRUE_DEPTH:
  291.     case TRUE_DEPTH+8:    /* I'm not sure whether the XBGR frame buffer
  292.                    returns depth 24 or 32. */
  293.          /* pseudo-color display */
  294.          xdev->color_info = truecolor_ci;
  295.          xdev->truecolor = 1;
  296.          break;
  297.     default:
  298.          eprintf1("gs: Cannot handle display of depth %d.\n",
  299.                   xdev->pw->pw_pixrect->pr_depth);
  300.          return -1;
  301.     }
  302.         
  303.     if ( gx_device_has_color(xdev)
  304. #ifndef FAKE_TRUE_COLOR
  305.          && !xdev->truecolor
  306. #endif
  307.        )
  308.        {    
  309.         int j;
  310.         int color;
  311.  
  312.         /*
  313.          * Create the pre-allocated colorcube.
  314.          */
  315.         xdev->red = (byte *)malloc(CMS_SIZE);
  316.         xdev->green = (byte *)malloc(CMS_SIZE);
  317.         xdev->blue = (byte *)malloc(CMS_SIZE);
  318.         if (!xdev->red || !xdev->green || !xdev->blue) {
  319.             eprintf("gs: no memory for colormap\n");
  320.             return -1;
  321.         }
  322.  
  323. #ifdef FAKE_TRUE_COLOR
  324.         /*
  325.          * Fit the largest possible color cube into the colormap.
  326.          */
  327.         for ( j = 0; j < ALL_COLS; j++ ) {
  328.            xdev->blue[j] =
  329.             (double)((j & TRUE_BLUE_MASK)
  330.                      >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
  331.             / (TRUE_BLUE_COLS - 1)
  332.             * (ALL_COLS - 1);
  333.            xdev->green[j] =
  334.             (double)((j & TRUE_GREEN_MASK) >> TRUE_RED_BITS)
  335.             / (TRUE_GREEN_COLS - 1)
  336.             * (ALL_COLS - 1);
  337.            xdev->red[j] =
  338.             (double)((j & TRUE_RED_MASK))
  339.             / (TRUE_RED_COLS - 1)
  340.             * (ALL_COLS - 1);
  341.         }
  342.  
  343.         xdev->freecols = 0;
  344. #else /* !FAKE_TRUE_COLOR */
  345.         /*
  346.          * Black and white are allocated in the last two slots,
  347.          * so as to be compatible with the monochrome colormap.
  348.          * This prevents most text etc. to go technicolor as focus
  349.          * changes into the ghostscript window.
  350.          *
  351.              * The requirement that these two entries be at the end
  352.          * of the colormap makes it most convenient to allocate
  353.          * the remmaining entries from back to the front as well.
  354.          * Therefore xdev->freecols is the minimal allocated
  355.          * color index, and decreases as new ones are allocated.
  356.          */
  357.         j = CMS_SIZE - 2;
  358.         cms_monochromeload(xdev->red + j,
  359.                            xdev->green + j,
  360.                    xdev->blue + j);
  361.  
  362.         /*
  363.          * The remaining slots down to CMS_SIZE - RGB_COLS are filled
  364.          * with evenly spaced points from the colorcube.
  365.          */
  366.         for ( color = 1; color < RGB_COLS - 1; color++ ) {
  367.            j--;
  368.            xdev->red[j] =
  369.             (double)((color & RED_MASK) >> (GREEN_BITS + BLUE_BITS))
  370.             / (RED_COLS - 1)
  371.             * (ALL_COLS - 1);
  372.            xdev->green[j] =
  373.             (double)((color & GREEN_MASK) >> BLUE_BITS)
  374.             / (GREEN_COLS - 1)
  375.             * (ALL_COLS - 1);
  376.            xdev->blue[j] =
  377.             (double)((color & BLUE_MASK))
  378.             / (BLUE_COLS - 1)
  379.             * (ALL_COLS - 1);
  380.         }
  381.  
  382.  
  383.         /*
  384.          * Set the low-water mark to the beginning of the colorcube.
  385.          */
  386.         xdev->freecols = j;
  387.  
  388.         /*
  389.          * The unused entries are filled so that the last entry is
  390.          * always different from the 0th entry.  This is a requirement
  391.          * for SunWindows.
  392.          */
  393.         for (j-- ; j >= 0 ; j--) {
  394.            xdev->red[j] = xdev->green[j] = xdev->blue[j] =
  395.             ~xdev->red[CMS_SIZE - 1];
  396.         }
  397. #endif /* FAKE_TRUE_COLOR */
  398.  
  399.         /*
  400.          * Install the colormap.
  401.          */
  402.         sprintf(xdev->cmsname, "%s-%d", CMSNAME, getpid());
  403.         pw_setcmsname(xdev->pw, xdev->cmsname);
  404.         pw_putcolormap(xdev->pw, 0, CMS_SIZE,
  405.                        xdev->red, xdev->green, xdev->blue);
  406.        }
  407.     else {
  408.         xdev->freecols = 0;
  409.         xdev->red = (byte *)0;
  410.         xdev->green = (byte *)0;
  411.         xdev->blue = (byte *)0;
  412.     }
  413.  
  414.     /*
  415.      * Reset to retained after colormap length is changed 
  416.      */
  417.     window_set(xdev->canvas, 
  418.         CANVAS_RETAINED,         TRUE,
  419.         WIN_VERTICAL_SCROLLBAR,        scrollbar_create(0),
  420.         WIN_HORIZONTAL_SCROLLBAR,    scrollbar_create(0),
  421.         0);
  422.     window_set(xdev->frame, WIN_SHOW, TRUE, 0);
  423.     /* Interpose a destroy function to keep Ghostscript from */
  424.     /* getting confused if the user closes the window. */
  425.     notify_interpose_destroy_func(xdev->frame, destroy_func);
  426.     (void) notify_do_dispatch();
  427.     (void) notify_dispatch();
  428.     return 0;
  429. }
  430. /* Prevent the user from closing the window. */
  431. private Notify_value
  432. destroy_func(Frame frame, Destroy_status status)
  433. {    if ( status == DESTROY_CHECKING )
  434.        {    notify_veto_destroy(frame);
  435.         return (NOTIFY_DONE);
  436.        }
  437.     return (notify_next_destroy_func(frame, status));
  438. }
  439.  
  440. /* Close the device. */
  441. int
  442. sun_close(gx_device *dev)
  443. {    window_destroy(xdev->frame);
  444.     xdev->frame = (Frame)0;
  445.     xdev->canvas = (Canvas)0;
  446.     xdev->pw = (Pixwin *)0;
  447.     xdev->freecols = 0;
  448.     if (xdev->red)
  449.         free(xdev->red);
  450.     if (xdev->green)
  451.         free(xdev->green);
  452.     if (xdev->blue)
  453.         free(xdev->blue);
  454.     return 0;
  455. }
  456.  
  457. /* Synchronize the display with the commands already given */
  458. int
  459. sun_sync(register gx_device *dev)
  460. {    (void) notify_dispatch();
  461.     return 0;
  462. }
  463.  
  464. /* Map RGB to color number -
  465.     Look for existing entry in colormap, or create a new one, or
  466.     give up if no free colormap entries (requesting dithering).
  467.  */
  468. gx_color_index
  469. sun_map_rgb_color(gx_device *dev, unsigned short red,
  470.     unsigned short green, unsigned short blue)
  471. {    if ( !gx_device_has_color(dev) )
  472.         /*
  473.          * Invert default color index to match mono display
  474.          * pixel values (black = 1, white = 0).
  475.          */
  476.         return !gx_default_map_rgb_color(dev, red, green, blue);
  477.     else if ( !xdev->truecolor ) {
  478.         byte red_val, green_val, blue_val;
  479.         int i;
  480.         static int warn = 1;
  481.  
  482.         /*
  483.          * Determine the RGB values at display resolution we
  484.          * ideally would want this color to be mapped into.
  485.          */
  486.         red_val = (double)red/gx_max_color_value * (ALL_COLS - 1);
  487.         green_val = (double)green/gx_max_color_value * (ALL_COLS - 1);
  488.         blue_val = (double)blue/gx_max_color_value * (ALL_COLS - 1);
  489.  
  490.         /*
  491.          * Look for an exact match among the colors already allocated.
  492.          * This includes the pre-allocated default color cube.
  493.          */
  494.         for (i = CMS_SIZE - 1; i >= xdev->freecols; i--) {
  495.             if (xdev->red[i] == red_val &&
  496.                 xdev->green[i] == green_val &&
  497.                 xdev->blue[i] == blue_val) {
  498.                 return i;
  499.             }
  500.         }
  501.         
  502.         /*
  503.          * If we run out of space in the color map, let gs know.
  504.          * It will call us again to request colors to do the
  505.          * dithering, and hopefully request only RGB values that
  506.          * match the colorcube entries. IF NOT, WE WILL LOOP
  507.          * FOREVER!
  508.          * NOTE: Leave the zero'th colormap entry alone lest the
  509.          * scrollbar be colored.
  510.          */
  511.         if (xdev->freecols <= 1) {
  512.             if (warn) {
  513.             eprintf("gs: last spare color map entry allocated\n");
  514.             warn = 0;
  515.             }
  516.             return gx_no_color_index; 
  517.         }
  518.  
  519.         /*
  520.          * Allocate new color in map.
  521.          */
  522.         xdev->red[i] = red_val;
  523.         xdev->green[i] = green_val;
  524.         xdev->blue[i] = blue_val;
  525.         pw_setcmsname(xdev->pw, xdev->cmsname);
  526.         pw_putcolormap(xdev->pw, i, 1,
  527.                        &xdev->red[i], &xdev->green[i], &xdev->blue[i]);
  528.         
  529.         xdev->freecols = i;
  530.         return i;
  531.     }
  532.     else {    /* true color mapping --
  533.             color index encodes all 3 RGB values */
  534.         return ((blue >> (gx_color_value_bits - TRUE_BLUE_BITS))
  535.             << (TRUE_GREEN_BITS + TRUE_RED_BITS)) |
  536.                ((green >> (gx_color_value_bits - TRUE_GREEN_BITS))
  537.             << TRUE_RED_BITS) |
  538.                (red >> (gx_color_value_bits - TRUE_RED_BITS));
  539.     }
  540. }
  541.  
  542. /* Map color number back to RGB values  - see sun_map_rgb_color(), above */
  543. int
  544. sun_map_color_rgb(gx_device *dev, gx_color_index color,
  545.     unsigned short rgb[3])
  546. {    if ( !gx_device_has_color(dev) )
  547.         return gx_default_map_color_rgb(dev, !color, rgb);
  548.         else if ( !xdev->truecolor ) {
  549.         /*
  550.          * We just use the colormap to map back to rgb values.
  551.          */
  552.         if (color < xdev->freecols || color >= CMS_SIZE) {
  553.             eprintf1("gs: attempt to get RGB values for unallocated color index %d\n", color);
  554.             return -1;
  555.         }
  556.         rgb[0] = (double)xdev->red[color] / (ALL_COLS - 1)
  557.              * gx_max_color_value;
  558.         rgb[1] = (double)xdev->green[color] / (ALL_COLS - 1)
  559.              * gx_max_color_value;
  560.         rgb[2] = (double)xdev->blue[color] / (ALL_COLS - 1)
  561.              * gx_max_color_value;
  562.         return 0;
  563.     }
  564.     else {    /* true color mapping */
  565.         rgb[0] = (double)((unsigned short)(color & TRUE_RED_MASK))
  566.              / (TRUE_RED_COLS - 1)
  567.              * gx_max_color_value;
  568.         rgb[1] = (double)((unsigned short)(color & TRUE_GREEN_MASK)
  569.               >> TRUE_RED_BITS)
  570.              / (TRUE_GREEN_COLS - 1)
  571.              * gx_max_color_value;
  572.         rgb[2] = (double)((unsigned short)(color & TRUE_BLUE_MASK)
  573.               >> (TRUE_GREEN_BITS + TRUE_RED_BITS))
  574.              / (TRUE_BLUE_COLS - 1)
  575.              * gx_max_color_value;
  576.         return 0;
  577.     }
  578. }
  579.  
  580. /* Fill a rectangle with a color. */
  581. int
  582. sun_fill_rectangle(register gx_device *dev,
  583.   int x, int y, int w, int h, gx_color_index color)
  584. {    fit_fill(dev, x, y, w, h);
  585.  
  586.     pw_write(xdev->pw, x, y, w, h, PIX_SRC | PIX_COLOR((int)(color)),
  587.          (Pixrect *)0, 0, 0);
  588.     (void) notify_dispatch();
  589.     return 0;
  590. }
  591.  
  592. /* Copy a monochrome bitmap. */
  593. int
  594. sun_copy_mono(register gx_device *dev,
  595.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  596.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  597. {
  598. /* We define a non-const pointer to the data so we can invert it or */
  599. /* byte-swap it in place temporarily (we restore it at the end). */
  600. /* Yes, this is a bad and wicked thing to do! */
  601. #define non_const_base ((byte *)base)
  602.  
  603.     register int i;
  604.     int nbytes;
  605.     extern struct pixrectops mem_ops;
  606. #if !arch_is_big_endian            /* need to swap bits & bytes */
  607. #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
  608.     byte swap_buf[BUF_WIDTH_BYTES];
  609. #endif
  610.  
  611.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  612.     nbytes = h * raster;
  613.  
  614.     xdev->pr.pr_ops = &mem_ops;
  615.     xdev->pr.pr_width = w + sourcex + 8;
  616.     xdev->pr.pr_height = h;
  617.     xdev->pr.pr_depth = 1;
  618.     xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
  619.     xdev->mpr.md_linebytes = raster;
  620.     xdev->mpr.md_image = (short *)((ulong)base & ~1);
  621. #if !arch_is_big_endian
  622.     /* Reverse the bit order in each byte. */
  623.     for ( i = 0; i < nbytes; i++ )
  624.         non_const_base[i] = reverse_bits[base[i]];
  625. #endif
  626.     pw_batch_on(xdev->pw);
  627.     if (one != gx_no_color_index)
  628.     {    pw_stencil(xdev->pw, x, y, w, h,
  629.             PIX_SRC | PIX_COLOR(one), &(xdev->pr),
  630.             ((int)base & 1) ? sourcex + 8 : sourcex, 0,
  631.             (Pixrect *)0, 0, 0);
  632.     }
  633.     if (zero != gx_no_color_index)
  634.     {    for (i = 0; i < nbytes; i++)
  635.             non_const_base[i] = ~base[i];
  636.         pw_stencil(xdev->pw, x, y, w, h,
  637.             PIX_SRC | PIX_COLOR(zero), &(xdev->pr),
  638.             ((int)base & 1) ? sourcex + 8 : sourcex, 0,
  639.             (Pixrect *)0, 0, 0);
  640.         for (i = 0; i < nbytes; i++)
  641.             non_const_base[i] = ~base[i];
  642.     }
  643.     pw_batch_off(xdev->pw);
  644. #if !arch_is_big_endian
  645.     /* Reverse the bits back again. */
  646.     for ( i = 0; i < nbytes; i++ )
  647.         non_const_base[i] = reverse_bits[base[i]];
  648. #endif
  649.     (void) notify_dispatch();
  650.     return 0;
  651. }
  652.  
  653. /* Copy a color bitmap. */
  654. int
  655. sun_copy_color(register gx_device *dev,
  656.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  657.   int x, int y, int w, int h)
  658. {
  659.     register int i;
  660.     extern struct pixrectops mem_ops;
  661.  
  662.     if ( !gx_device_has_color(dev) )
  663.         return sun_copy_mono(dev, base, sourcex, raster, id,
  664.                      x, y, w, h,
  665.                      (gx_color_index)0, (gx_color_index)1);
  666.  
  667.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  668.  
  669.     xdev->pr.pr_ops = &mem_ops;
  670.     xdev->pr.pr_width = w + sourcex + 8;
  671.     xdev->pr.pr_height = h;
  672.     xdev->pr.pr_depth = 8;
  673.     xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
  674.     xdev->mpr.md_linebytes = raster;
  675.     xdev->mpr.md_image = (short *)((ulong)base & ~1);
  676.     pw_write(xdev->pw, x, y, w, h,
  677.          PIX_SRC, &(xdev->pr),
  678.          (((int)base & 1) ? sourcex + 8 : sourcex), 0);
  679.     (void) notify_dispatch();
  680.     return 0;
  681. }
  682.  
  683. /* Draw a line */
  684. int
  685. sun_draw_line(register gx_device *dev,
  686.   int x0, int y0, int x1, int y1, gx_color_index color)
  687. {    pw_vector(xdev->pw, x0, y0, x1, y1, PIX_SRC, color);
  688.     (void) notify_dispatch();
  689.     return 0;
  690. }
  691.